package com.ce.pms.system.custservice.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.base.vo.SearchVO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.system.custservice.dao.IEmergencyDegreeDao;
import com.ce.pms.system.custservice.model.EmergencyDegreeDO;
import com.ce.pms.system.custservice.service.IEmergencyDegreeService;
import com.ce.pms.system.custservice.vo.EmergencyDegreeVO;
import com.ce.util.BeanUtil;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;

/**
 * 功能描述: 紧急程度维护服务SERVICE实现类
 * <p>
 * 版权所有：中企动力
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 胡煦 Create on:2008-12-18 Edit author: Edit on: 修改原因：
 *
 */

public class EmergencyDegreeServiceImpl extends PmsBaseServiceImpl implements
		IEmergencyDegreeService {

	/**
	 * 方法描述: 分页查询所有紧急程度列表
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param pageNo,pageSize,order
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllEmergencyDegreeByPage(SearchVO searchVO){
		Page page = emergencyDegreeDao.queryAllEmergencyDegreeByPage(searchVO);
		List<EmergencyDegreeDO> emergencyDegreeDOlist = page.getResult();
		List<EmergencyDegreeVO> emergencyDegreeVOlist = new ArrayList<EmergencyDegreeVO>();
		for(EmergencyDegreeDO emergencyDegreeDO : emergencyDegreeDOlist){
			EmergencyDegreeVO emergencyDegreeVO = this.assembleD2V(emergencyDegreeDO, null);
			emergencyDegreeVOlist.add(emergencyDegreeVO);
		}
		page.setResult(emergencyDegreeVOlist);
		return page;
	}
	
	/**
	 * 功能描述:  <br>
	 * @param emergencyId
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-4  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public EmergencyDegreeDO getEmergencyDegreeDOById(Long emergencyId){
		return this.emergencyDegreeDao.loadById(emergencyId);
	}
	
	/**
	 * 方法描述: 紧急程度列表查询方法
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param name
	 * @return List<EmergencyDegreeVO>
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public List<EmergencyDegreeVO> getEmergencyDegreeList() {
		Page page = emergencyDegreeDao.getEmergencyDegreeList(null);
		List<EmergencyDegreeDO> emergencyDegreeDOList = page.getResult();
		List<EmergencyDegreeVO> emergencyDegreeVOList = new ArrayList<EmergencyDegreeVO>();
		for(EmergencyDegreeDO emergencyDegreeDO : emergencyDegreeDOList){
			EmergencyDegreeVO emergencyDegreeVO = this.assembleD2V(emergencyDegreeDO, null);
			emergencyDegreeVOList.add(emergencyDegreeVO);
		}
		return emergencyDegreeVOList;
	}
	/**
	 * 方法描述: 根据ID查询VO方法
	 * @author 胡煦
	 * Create on:2008-12-22
	 * @param keyword
	 * @return EmergencyDegreeVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public EmergencyDegreeVO findEmergencyDegreeVOById(Long keyword) {
		if(BlankUtil.isBlank(keyword)){
			return null;
		}
		return this.assembleD2V(emergencyDegreeDao.findById(keyword), null);
	}
	/**
	 * 方法描述: 紧急程度增加方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param emergencyDegreeVO
	 * @return EmergencyDegreeVO Edit author: Edit on: 修改原因：
	 */
	public EmergencyDegreeVO addEmergencyDegree(EmergencyDegreeVO emergencyDegreeVO) {
		if(BlankUtil.isBlank(emergencyDegreeVO)){
			return null;
		}
		this.checkEmergencyDegreeByName(emergencyDegreeVO.getName(), null);
		EmergencyDegreeDO emergencyDegreeDO = this.assembleV2D(emergencyDegreeVO, null);
		emergencyDegreeDO = emergencyDegreeDao.saveObject(emergencyDegreeDO);
		emergencyDegreeVO.setKeyword(emergencyDegreeDO.getKeyword());
		return emergencyDegreeVO;
	}
	/**
	 * 方法描述: 紧急程度删除方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param emergencyDegreeId
	 * @return boolean Edit author: Edit on: 修改原因：
	 */
	public boolean deleteEmergencyDegree(Long emergencyDegreeId) {
		if(BlankUtil.isBlank(emergencyDegreeId)){
			return false;
		}
		try {
			emergencyDegreeDao.deleteObjectById(emergencyDegreeId);
		} catch (RuntimeException e) {
			e.printStackTrace();
			throw new BusinessException(
					"custservice:system_delete_inused_exception",
					BusinessException.WARNING);
		}
		return true;
	}
	/**
	 * 方法描述: 紧急程度修改方法
	 *
	 * @author 胡煦 Create on:2008-12-16
	 * @param emergencyDegreeVO
	 * @return EmergencyDegreeVO Edit author: Edit on: 修改原因：
	 */
	public EmergencyDegreeVO editEmergencyDegree(EmergencyDegreeVO emergencyDegreeVO) {
		if(BlankUtil.isBlank(emergencyDegreeVO)){
			return null;
		}
		this.checkEmergencyDegreeByName(emergencyDegreeVO.getName(), emergencyDegreeVO.getKeyword());
		EmergencyDegreeDO emergencyDegreeDO = this.assembleV2D(emergencyDegreeVO, null);
		EmergencyDegreeDO oldEmergencyDegreeDO = emergencyDegreeDao.findById(emergencyDegreeVO.getKeyword());
		modifyInfo(oldEmergencyDegreeDO.getSysInfo(), emergencyDegreeVO.getModifier());
		BeanUtil.convertVoAsDo(emergencyDegreeDO, oldEmergencyDegreeDO);
		return emergencyDegreeVO;
	}
	/**
	 * 功能描述:校验紧急程度名字是否存在<br>
	 * 注：新建判断name的唯一性时，oldId传为null；修改判断name的唯一性时，则需要传入本身的ID<br>
	 * @param name
	 * @param id
	 * @return
	 * Create author:胡煦
	 * Create on:2008-7-18
	 * Edit author:
	 * Edit on:
	 * Why:
	 */
	public void checkEmergencyDegreeByName(String name,Long id){
		Long count = emergencyDegreeDao.countEmergencyDegreeByName(name, id);
		if(count > 0){
			throw new BusinessException(
					"custservice:system_name_exist_exception",
					BusinessException.WARNING);
		}
	}

	/**
	 * 方法描述: EmergencyDegreeVO To EmergencyDegreeDO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param emergencyDegreeVO
	 * @param emergencyDegreeDO
	 * @return EmergencyDegreeDO
	 */
	private EmergencyDegreeDO assembleV2D(EmergencyDegreeVO emergencyDegreeVO, EmergencyDegreeDO emergencyDegreeDO) {
		if(BlankUtil.isBlank(emergencyDegreeDO)){
			emergencyDegreeDO = new EmergencyDegreeDO();
		}
		BeanUtil.convertVoAsDo(emergencyDegreeVO, emergencyDegreeDO);
		if(!BlankUtil.isBlank(emergencyDegreeVO.getCreator())){
			emergencyDegreeDO.setSysInfo(createInfo(emergencyDegreeVO.getCreator()));
		}
		return emergencyDegreeDO;
	}

	/**
	 * 方法描述: EmergencyDegreeDO To EmergencyDegreeVO
	 * @author 胡煦
	 * Create on:2008-12-16
	 * @param emergencyDegreeDO
	 * @param emergencyDegreeVO
	 * @return EmergencyDegreeVO
	 */
	private EmergencyDegreeVO assembleD2V(EmergencyDegreeDO emergencyDegreeDO, EmergencyDegreeVO emergencyDegreeVO) {
		if(BlankUtil.isBlank(emergencyDegreeVO)){
			emergencyDegreeVO = new EmergencyDegreeVO();
		}
		PopulateUtil.populate(emergencyDegreeDO, emergencyDegreeVO);
		return emergencyDegreeVO;
	}
	private IEmergencyDegreeDao emergencyDegreeDao;
	/**
	 * @param emergencyDegreeDao the emergencyDegreeDao to set
	 */
	public void setEmergencyDegreeDao(IEmergencyDegreeDao emergencyDegreeDao) {
		this.emergencyDegreeDao = emergencyDegreeDao;
	}

}
